package com.easylive.admin.controller;

import com.easylive.entity.enums.UserStatusEnum;
import com.easylive.entity.po.UserInfo;
import com.easylive.entity.query.UserInfoQuery;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.service.UserInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @Author: yudaxian
 * @DateTime: 2024/12/31 16:46
 **/
@RequestMapping("/user")
@RestController
public class UserController extends ABaseController {

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/loadUser")
    public ResponseVO loadUser(@Validated Integer pageNo, Integer pageSize, Integer status, String nickNameFuzzy) {
        UserInfoQuery userInfoQuery = new UserInfoQuery();
        userInfoQuery.setPageNo(pageNo);
        userInfoQuery.setPageSize(pageSize);
        userInfoQuery.setOrderBy("last_login_time desc");
        userInfoQuery.setStatus(status);
        userInfoQuery.setNickNameFuzzy(nickNameFuzzy);
        return getSuccessResponseVO(userInfoService.findListByPage(userInfoQuery));
    }

    @PostMapping("/changeStatus")
    public ResponseVO changeStatus(@Validated @NotBlank String userId, @NotNull Integer status) {
        UserStatusEnum statusEnum = UserStatusEnum.getByCode(status);
        UserInfoQuery userInfoQuery = new UserInfoQuery();
        userInfoQuery.setUserId(userId);
        userInfoQuery.setStatus(statusEnum == UserStatusEnum.DISABLED ? UserStatusEnum.NORMAL.getCode() : UserStatusEnum.DISABLED.getCode());
        UserInfo updateInfo = new UserInfo();
        updateInfo.setStatus(status);

        userInfoService.updateByParam(updateInfo, userInfoQuery);

        return getSuccessResponseVO(null);
    }

}
